package edu.jsnu.operate.controller.ticket;

import beans.PageQuery;
import com.github.pagehelper.PageInfo;
import dto.resDto.JsonData;
import dto.resDto.RespData;
import edu.jsnu.operate.controller.ticket.common.AbstractController;
import edu.jsnu.service.ticket.order.dto.OrderShow;
import edu.jsnu.service.ticket.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "ticket/order")
public class OrderController extends AbstractController {

    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "order.page")
    public String orderPage(){
        return "ticket/order/order";
    }

    @RequestMapping(value = "save.page")
    public String savePage(Model model, @RequestParam(value = "id", required = false)Integer id){
        OrderShow orderShow = orderService.getOrderInfoById(id);
        model.addAttribute("order", orderShow);
        return "ticket/order/save";
    }

    @RequestMapping(value = "order.json", method = RequestMethod.GET)
    @ResponseBody
    public RespData orderData(PageQuery pageQuery){
        try {
            List<OrderShow> orderList = orderService.getAllOrder(pageQuery);
            PageInfo<OrderShow> pageInfo = new PageInfo<>(orderList);
            return RespData.success(pageInfo.getList(), pageInfo.getTotal());
        }catch (Exception e){
            e.printStackTrace();
        }
        return RespData.fail("查找订单数据异常，请联系管理员");
    }

    @RequestMapping(value = "save.json", method = RequestMethod.POST)
    @ResponseBody
    public JsonData saveOrder(@RequestParam(value = "id") Integer id, @RequestParam(value = "status") Integer status){
        try{
            return orderService.updateOrderStatusByAdmin(id, status)>0?JsonData.success("修改订单状态成功"):JsonData.fail("修改订单状态失败，请稍后再试");
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonData.fail("修改订单状态异常，请联系管理员");
    }
}
